var code    = '';
$(function (){
    $('#getCode').on('click',function (e){
        e.preventDefault();
        //TODO 等待60s后才能再次点击
        //
        var email = $('#email').val();
        if(email !== ''){
            $.ajax({
                type     : 'POST',
                url      : 'http://localhost:8080/verification',
                data    : {
                    getCode       : 'getVerification',
                    email         : email
                },
                success : function (data){
                    code = data;
                    console.log(data);
                },
                error   : function (errorMsg){
                    console.log(errorMsg);
                }
            });
        }else{
            $('#error-msg').text('The email cannot be empty"');
        }

    })
    $('#loginByEmail').on('click',function (e){
        e.preventDefault();
        $.ajax({
            type     : 'POST',
            url      : 'http://localhost:8080/signOn',
            data    : {
             email   : $('#email').val(),
             token   : $('#token').val(),
             login   : 'loginByEmail'
            },
            success : function (data){
                if(data.indexOf("address") !== -1){
                    console.log(data);
                    window.location.href='http://localhost:8080/' + data.substring(data.indexOf(":") + 1, data.length);
                }else{
                    //显示
                    $('#error-msg').text(data);
                    console.log('register ='+data);
                }
            }
        });

    })

    $('#email').on('blur',function (){
        var email = $(this).val();
        if(email === ''){
            $('#error-msg').text('The email cannot be empty"');
        }else if(email.indexOf("@") === -1){
            $('#error-msg').text('Please enter a reasonable email');
        }else{
            $('#error-msg').text('');
        }
    });
    $('#token').on('blur',function (){
        var token = $(this).val();
        if(code === ''){
            $('#error-msg').text('Please get a verification first');
        }else if(token === ''){
            $('#error-msg').text('The token cannot be empty"');
        }else{
            $('#error-msg').text('');
        }
    });
})